// ClientInterface class
// =====================
// Connects the Client to the Server and to HammurabiGame

#pragma once

#include "..\Shared\Message.h"
#include "WinsockInclude.h"
#include "..\Shared\WSAErrors.h"
#include <iostream>
#include <string>
#include <iomanip>
#include "View.h"

using namespace std;

//Needs to be the same as ServerInterface
#define SRVR_ADDRESS "127.0.0.1"
#define SRVR_PORT 7654
#define BUFFER_SIZE 512

#define CLIENTINTERFACE ClientInterface::Instance()

class ClientInterface
{
	// union to store all message types so we know the largest size
#pragma pack(1)
	union
	{
		Message						mMessage;
		Msg_HasCoordinate			mHasCoordinate;
		Msg_HasCoordinateAndObject	mHasCoordAndObj;
		Msg_HasCoordinateAndInt		mHasCoordAndInt;
		Msg_HasInt					mHasInt;
		Msg_HasLRS					mHasLRS;
		Msg_HasPlayerInfo			mHasPlayerInfo;
	} recv_packet, send_packet;
#pragma pack()


// member variables
	SOCKET myServerSocket;	

// member functions
private:
	ClientInterface(void);
	ClientInterface(const ClientInterface&);
	ClientInterface* operator=(const ClientInterface&);
	~ClientInterface(void);

	void CleanWSA();
	void ShutAndCleanSocket(SOCKET s);

public:
	// called during initialization
	// connects to the server and returns the initial game state
	bool ConnectToServer(const char* ipaddress = SRVR_ADDRESS);

	// sends the next turn data to the server, waits for a
	// response from the server and hands the new data back
	// to the view
	void SendToServer(Message* msg);
	Message* ReceiveFromServer(void);

	// shuts down the clientinterface once the user quits, or there is an error
	void ShutDown(void);
	
	// accesses the singleton
	static ClientInterface* Instance(void);

	// asks the user for the server's IP
	string GetServerIP(void);
};